/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.swirrel.installer;

import java.awt.Component;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionListener;
import org.swirrel.annotation.ValueChanged;
import org.swirrel.listener.SwirrelTreeSelectionListener;
import org.swirrel.util.MethodCall;

/**
 *
 * @author Gronau
 */
public class TreeSelectionListenerInstaller extends AbstractListenerInstaller implements SwirrelListenerInstaller {

    public TreeSelectionListenerInstaller() {
        super(Arrays.<Class<? extends Annotation>>asList(ValueChanged.class),
                Arrays.<Class<?>>asList(JTree.class));
    }

    public boolean isListenerAlreadySet(Object obj) {
        TreeSelectionListener[] treeSelectionListeners = ((JTree) obj).getTreeSelectionListeners();
        for (TreeSelectionListener lsl : treeSelectionListeners) {
            if (lsl instanceof SwirrelTreeSelectionListener) {
                return true;
            }
        }
        return false;
    }

    public void registerListener(Field field, Component comp, List<MethodCall> methodCalls) throws IllegalAccessException {
        Object obj = field.get(comp);
        TreeSelectionListener tsl = new SwirrelTreeSelectionListener(methodCalls.get(0));
        ((JTree) obj).addTreeSelectionListener(tsl);
    }
}

